//=============================================================================
// HelloDirect3D.cpp by Frank Luna (C) 2005 All Rights Reserved.
//
// Demonstrates Direct3D Initialization and text output using the 
// framework code.
//=============================================================================

#include "FastCar.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
				   PSTR cmdLine, int showCmd)
{
	// Enable run-time memory check for debug builds.
	#if defined(DEBUG) | defined(_DEBUG)
		_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	#endif


	FastCar app(hInstance, "Fast Car", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);
	gd3dApp = &app;
	gd3dApp->enableFullScreenMode(false);

	return gd3dApp->run();
}

FastCar::FastCar(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
: D3DApp(hInstance, winCaption, devType, requestedVP)
{
	//srand(time_t(0));
	if(!checkDeviceCaps())
	{
		MessageBox(0, L"checkDeviceCaps() Failed", 0, 0);
		PostQuitMessage(0);
	}

	InitAllVertexDeclarations();

	onResetDevice();
	
}

FastCar::~FastCar()
{
	DestroyAllVertexDeclarations();
	ResourceManager::GetInstance()->Release();
	//DirectInput::GetInstance()->Release();
}

bool FastCar::checkDeviceCaps()
{
	// Nothing to check.
	return true;
}

void FastCar::onLostDevice()
{
	
}

void FastCar::onResetDevice()
{	
	CCamera::GetInstance()->BuildProjMtx();
}

void FastCar::updateScene(float dt)
{	
	CCamera::GetInstance()->Update(dt);
	CCamera::GetInstance()->BuildViewMtx();

	// Main
	StateManagement::GetInstance()->Update(dt);
}

void FastCar::drawScene()
{
	gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);

	RECT formatRect;
	GetClientRect(mhMainWnd, &formatRect);

	gd3dDevice->BeginScene();
	
	
	//TODO: Draw here
	StateManagement::GetInstance()->Render();

	gd3dDevice->EndScene();
	gd3dDevice->Present(0, 0, 0, 0);
}